;	Tested; only known bug is that the multiplayer menu is used in single player by default

;	Menu Hack
;	If co-op data has been loaded in multiplayer mode and the team hasn't warped,
;	enable warping to Gem Quarry (warping to Gem Quarry doesn't work; actually using
;	location 0x0010 instead) via L button
;	(If warping is done, set 0x800B67F4 to 0xDEADBEEF)

;	Begin C0DE segment
.long				0xC0DE0000
.long				0x00000000

LUI	S0,	0x800A		;
LBU	S0,	0x5060	(S0)	;S0 = menu mode byte
ORI	S1,	R0,	0x0001	;S1 = multiplayer mode value
LUI	S2,	0x800A		;S2 = 0x800A0000
;-------------------------------;Don't do anything if not in multiplayer mode
BNE	S0,	S1,		Menu_Hack_Exit
NOP
LBU	S3,	0x4FD0	(S2)	;S3 = number of players
SB	S3,	0x325D	(S2)	;Force split screen in multi

LUI	S4,	0x800B		;
LW	S5,	0x67F8	(S4)	;Don't do anything if co-op data hasn't been loaded
BEQZ	S5,			Menu_Hack_Exit
LW	S5,	0x67F4	(S4)	;
LUI	S6,		0xDEAD	;
ORI	S6,	S6,	0xBEEF	;Don't do anything if warping has been done
BEQ	S5,	S6,		Menu_Hack_Exit

LUI	S0,	0x8010		;
LH	S0,	0x5304	(S0)	;
ANDI	S0,	S0,	0x0020	;L button status in S0

LUI	S1,	0x800B		;S1 = 0x800B0000
LH	S2,	0x67F0	(S1)	;Previous L button status in S2
SH	S0,	0x67F0	(S1)	;Update previous status

XORI	S2,	S2,	0x0020	;last_keys = ~last_keys
AND	S0,	S0,	S2	;keys_down = ~last_keys & keys

ORI	S3,	R0,	0x0001	;S3 = true

;	L newly pressed set to true
BEQZ	S0,			Menu_Hack_Exit
NOP
SH	S3,	0x67F2	(S1)	;
LUI	S3,		0xDEAD	;
ORI	S3,	S3,	0xBEEF	;
SW	S3,	0x67F4	(S1)	;Kill this code before it crashes the game!

				Menu_Hack_Exit:
JR	RA			;
NOP

.align				3
.long				0xE0000000
.long				0x00000000
;	End C0DE segment

;	"If L newly pressed" part of code
.long				0xD10B67F2
.long				0x00000001
;	ERROR: This writes 0xFF, or at least the 0x01 becomes 0xFF;
;	it must be changed back to 0x01 once (and only once) to keep the emulator
;	from crashing
.long				0x800A3294
.long				0x00000001
;	FIXME: 0x0110 (Gem Quarry) is more aesthetic but doesn't work,
;	but 0x0010 (cave on Goldwood) is an ugly starting point (although it does work)
;	NOTE: 0x0010 doesn't work very often in PJ 64, but 0x0016 (Walkway) and 0x0110 do
;	NOTE: 0x0016 works in Mupen as well, so switching to that
.long				0x810A3250
.long				0x00000016
;	Tell other code to fix bug
.long				0x820B67EC
.long				0xDEADBEEF
;	Set L as not pressed
.long				0x810B67F2
.long				0x00000000
.long				0xE0000000
.long				0x00000000

;	Handle bug of previous code
.long				0xD20B67EC
.long				0xDEADBEEF
.long				0xD00A3294
.long				0x000000FF
.long				0x800A3294
.long				0x00000001
;	Have this code disable itself
.long				0x820B67EC
.long				0x00000000
.long				0xE0000000
.long				0x00000000
.long				0xE0000000
.long				0x00000000

;	If team has warped, enable single player menu in multiplayer
.long				0xD20B67F4
.long				0xDEADBEEF
.long				0xD6046158
ORI	T9,	R0,	0x0000	;Make sure code being written isn't already there
.long				0x82046158
;	Was
;LBU	T9,	0x4CF4	(T9)	;Load mode byte to determine which pause menu to draw
;	Is now
ORI	T9,	R0,	0x0000	;Assumes mode to be single player for menu drawing
.long				0xE0000000
.long				0x00000000
.long				0xE0000000
.long				0x00000000

;	If mode is single player, disable single player menu for multi
.long				0xD00A5060
.long				0x00000000
.long				0xD6046158
LBU	T9,	0x4CF4	(T9)	;Make sure code being written isn't already there
.long				0x82046158
LBU	T9,	0x4CF4	(T9)	;Load mode byte to determine which pause menu to draw
.long				0xE0000000
.long				0x00000000
.long				0xE0000000
.long				0x00000000
;	End of Menu Hack
